package com.mairuide._frame.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.wltea.analyzer.cfg.DefaultConfig;
import org.wltea.analyzer.dic.Dictionary;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;

@Configuration
public class MVCConfig extends WebMvcConfigurerAdapter {
	@Value("${uploadFilePath}")
	private String filePath; //配置文件配置的物理保存地址
	@Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(
                Charset.forName("UTF-8"));
        return converter;
    }
	
	@Bean
    public IKAnalyzer ikAnalyzer(){
        // 实例化IKAnalyzer分词器
        Dictionary.initial(DefaultConfig.getInstance());
        IKAnalyzer ikAnalyzer = new IKAnalyzer();
        return ikAnalyzer;
    }
	
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);
		converters.add(responseBodyConverter());
	}
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		String mapPath = new File("").getAbsolutePath();
        registry.addResourceHandler("/file/**").addResourceLocations("file:"+filePath+"/report/");
		registry.addResourceHandler("/map/**").addResourceLocations("file:"+mapPath+"/static/maptile/");
       super.addResourceHandlers(registry);
	}
}
